import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken, removeToken, getCasToken, removeCasToken, setCasToken } from '@/utils/auth' // get token from cookie
import { getQueryString } from "@/utils/util"
import routes from '#/router/extra'

import store from '#/store'
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

NProgress.configure({
    showSpinner: false
}) // NProgress Configuration
const whiteList = [
    '/login',
    '/casLogin',
    '/404',
    '/screen',
]; // no redirect whitelist

Vue.use(VueRouter)

//  该路由对象不需要权限
const constantRoutes = [
    {
        path: '/login',
        component: () => import('@/views/login/Login'),
        name: 'login',
        hidden: true
    },
    // {
    //     path: '/casLogin',
    //     name: 'casLogin',
    //     component: () => import('@/views/login/casLogin'),
    //     hidden: true
    // },
    // 404
    {
        path: '/404',
        name: '404',
        component: () => import('@/views/unusual/NotFound'),
        hidden: true
    },
    {
        path: '/403',
        name: '403',
        component: () => import('@/views/unusual/403'),
        hidden: true
    },
    // {
    //     path: '/screen',
    //     component: () => import('@/views/login/screen'),
    //     name: 'screen',
    //     hidden: true
    // },
]

const router = new VueRouter({
    scrollBehavior: () => ({
        y: 0
    }),
    routes: constantRoutes.concat(routes)
})

function beforeEnter(to, from, next) {
    NProgress.start();
    const hasToken = getToken();
    if (hasToken) { // 判断是否有token
        if (to.path === '/login') {
            next({ path: '/' });
        } else {
            if (!store.state.user.userInfo.name) {
                store.dispatch('getUserInfo').then(() => {
                    next({ ...to, replace: true });
                })
            } else {
                next();
            }
        }
    } else {
        /* has no token */
        if (whiteList.indexOf(to.path) !== -1) {
            // 在免登录白名单，直接进入
            next();
        } else {
            removeToken()
            next('/login') // 否则全部重定向到登录页
            NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
        }
    }
}


router.beforeEach(async (to, from, next) => {
    let newCasToken = getQueryString("token");
    let type = getQueryString("type");
    let casToken = getCasToken();
    if (newCasToken) {
        if (newCasToken == casToken) {
            beforeEnter(to, from, next);
        } else {
            store.dispatch('otherLogin', {
                subSystem: PROJECT.subSystem,
                token: newCasToken,
                type
            }).then(() => {
                setCasToken(newCasToken);
                next({ ...to, replace: true });
            }).catch(err => {
                history.go(-1);
            })
        }
    } else {
        beforeEnter(to, from, next);
    }
})

router.afterEach(() => {
    NProgress.done() // finish progress bar
})

export function resetRouter(r) {
    let createRouter = () => new VueRouter({
        scrollBehavior: () => ({
            y: 0
        }),
        routes: constantRoutes
    })
    let newRouter = createRouter();
    r.matcher = newRouter.matcher;
}

export default router